import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import Home from '../views/Home.vue'
import groupsRoute from './modules/groups'
import salesRoute from './modules/sales'


// import { getToken } from '@/utils/auth'

Vue.use(VueRouter)

const routes = [
  {
    path: '/auth',
    name: 'auth',
    meta: {
      title: '用户授权'
    },
    component: () => import('@/components/auth/Auth')
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: '亚电新能源',
      keepAlive: true,
    },
  },
  groupsRoute,
  ...salesRoute,

  
  {
    path: '/error',
    name: 'error',
    meta: {
      title: '错误提示'
    },
    component: () => import('@/components/Error')
  },
  { path: '*', redirect: '/404' }
]

const router = new VueRouter({
  routes
})


// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  document.title = to.matched[0].meta.title
  const openid = localStorage.getItem('openid');
  // next()
  if (!openid) {
    if (to.path === '/auth') {
      next()
    } else {
      localStorage.setItem('to_url', to.fullPath)
      next('/auth')
    }
  } else {
     //
     if (store.state.groups_id == 0) {
      store.dispatch('getUserInfo').then(() => {

      }).catch(error => {
        console.log(error.messge);
      })
    }
    next()
  }

})

export default router
